package com.roc.template.mongo.service.impl;

import com.roc.template.base.PageBean;
import com.roc.template.mongo.base.BasePageable;
import com.roc.template.mongo.base.BaseServiceImpl;
import com.roc.template.mongo.base.PageModel;
import com.roc.template.mongo.model.RemoteUser;
import com.roc.template.mongo.service.RemoteUserService;
import com.roc.template.mongo.vo.RemoteUserVo;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserService
 * @Description: TODO
 * @Author Administrator
 * @Date 2020/7/15 17 01
 * @Version V1.0
 **/
@Service
public class RemoteUserServiceImpl extends BaseServiceImpl<RemoteUser> implements RemoteUserService {

    @Resource
    private MongoTemplate mongoTemplate;

    public PageBean<RemoteUser> pageQuery(RemoteUserVo remoteUserVo) {
        BasePageable basePageable = new BasePageable();
        PageModel pageModel = new PageModel();
        Query infoQuery = new Query(Criteria.where("isDelete").is(false));

        List<Sort.Order> orders = new ArrayList<>();  //排序
        orders.add(new Sort.Order(Sort.Direction.DESC, "isDelete"));
        Sort sort = new Sort(orders);

        pageModel.setPageNum(remoteUserVo.getPageNum());
        pageModel.setPageSize(remoteUserVo.getPageSize());
        pageModel.setSort(sort);

        basePageable.setPage(pageModel);

        long count = mongoTemplate.count(infoQuery, RemoteUser.class);

        List<RemoteUser> inTimeInventories = mongoTemplate.find(infoQuery.with(basePageable), RemoteUser.class);

        PageImpl<RemoteUser> itemClasses = new PageImpl<>(inTimeInventories, basePageable, count);
        return new PageBean<>(itemClasses);
    }



    public RemoteUser getByCreateUser(String creatorwbUserId) {
        Query infoQuery = new Query(Criteria.where("isDelete").is(false).and("creatorWbUserId").is(creatorwbUserId));
        return mongoTemplate.findOne(infoQuery, RemoteUser.class);
    }

    public RemoteUser getByToUser(String towbUserId) {
        Query infoQuery = new Query(Criteria.where("isDelete").is(false).and("toWbUserId").is(towbUserId));
        return mongoTemplate.findOne(infoQuery, RemoteUser.class);
    }

    public void update(RemoteUser remoteUser) {
        Query infoQuery = new Query(Criteria.where("id").is(remoteUser.getId()))
                .addCriteria(Criteria.where("isDelete").is(false));
        Update update = new Update().set("createRefreshTime",remoteUser.getCreateRefreshTime())
                .set("toRefreshTime", remoteUser.getToRefreshTime());
        mongoTemplate.updateMulti(infoQuery,update, RemoteUser.class);
    }
}
